<template>
  <view class="pay">
    <!-- 1 收货地址 -->
    <view class="address u-p-20">
      <view class="address-btn u-flex u-row-center" v-if="!address">
        <u-button
          shape="circle"
          type="error"
          plain
          @click="handelChooseAddress"
          size="medium"
          :custom-style="{ backgroundColor: '#fff!important' }"
          >获取收货地址</u-button
        >
      </view>

      <view class="address-detail" v-else>
        <view class="address-detail">{{ addressData }}</view>
        <view class="address-user">{{
          address.userName + address.telNumber
        }}</view>
      </view>
    </view>

    <u-line color="#eee"></u-line>

    <!-- 2 已选商品 -->
    <view class="goods-list u-p-15">
      <view class="goods-list-title">已选商品</view>
      <view class="goods-list-content">
        <view
          class="cart-item"
          v-for="item in checkedCartList"
          :key="item.goods_id"
        >
          <!-- 2 图片 -->
          <view class="goods-img">
            <u-image
              width="191rpx"
              height="191rpx"
              :src="item.goods_small_logo"
            ></u-image>
          </view>
          <!-- 3 信息 -->
          <view class="goods-info">
            <!-- 名称 -->
            <view class="goods-name u-line-2">{{ item.goods_name }}</view>
            <!-- 价格和数量 -->
            <view class="goods-price-num">
              <view class="goods-price">￥{{ item.goods_price }}</view>
              <view class="goods-num-tool">
                <view class="goods-vnum">X{{ item.num }}</view>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>

    <!-- 2 统计 -->
    <view class="statistics">
      <view class="all-price">
        合计: <text>￥{{ checkedCartPrice }}</text>
      </view>
      <view class="all-count">
        <u-button type="error" shape="circle" @click="handleGOpay"
          >去支付({{ checkedCartNum }})</u-button
        >
      </view>
    </view>
  </view>
</template>

<script>
import { mapGetters, mapActions, mapState } from "vuex";
export default {
  data() {
    return {
      address: null,
    };
  },
  computed: {
    ...mapGetters("cart", [
      "checkedCartList",
      "checkedCartNum",
      "checkedCartPrice",
    ]),
    ...mapState("user", ["token"]),
    // 渲染地址
    addressData() {
      if (!this.address) return "";
      const { provinceName, cityName, countyName, detailInfo } = this.address;
      // console.log(provinceName + cityName + countyName + detailInfo);
      return provinceName + cityName + countyName + detailInfo;
    },
  },

  methods: {
    ...mapActions("user", ["getToken"]),
    async handelChooseAddress() {
      const [err, res] = await uni.chooseAddress();
      //   console.log(res);
      this.address = res;
    },
    async handleGOpay() {
      //#region
      if (!this.token) {
        await this.getToken();
      }
      //#endregion
      //#region  2 获取订单编号
      const oderParams = {
        order_price: this.checkedCartPrice,
        consignee_addr: this.addressData,
        goods: this.checkedCartList.map((v) => ({
          goods_id: v.goods_id,
          goods_number: v.num,
          goods_price: v.goods_price,
        })),
      };

      // console.log(oderParams);

      const result4 = await this.$u.post("/my/orders/create", oderParams);
      console.log("获取订单编号", result4);
      const { order_number } = result4.message;
      console.log(order_number);

      //#endregion

      //#region  3 获取支付参数
      const result5 = await this.$u.post("/my/orders/req_unifiedorder", {
        order_number,
      });
      // console.log(result5);
      const { pay } = result5.message;
      console.log(pay);
      //#endregion

      //#region   4 发起微信支付
      await uni.requestPayment(pay);
      //#endregion
    },
  },
};
</script>

<style lang="scss">
.pay {
  padding-bottom: 111rpx;
}
.cart-item {
  display: flex;
  padding: 10rpx;
  border-bottom: 1rpx solid #ccc;

  .goods-img {
    flex: 2;

    display: flex;
    align-items: center;
    justify-content: center;
  }

  .goods-info {
    color: #666;
    flex: 3;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    .goods-name {
    }

    .goods-price-num {
      display: flex;
      justify-content: space-between;
      .goods-price {
        color: #eb4450;
      }

      .goods-num-tool {
        width: 140rpx;
        height: 40rpx;

        display: flex;
        justify-content: flex-end;

        .goods-vnum {
        }
      }
    }
  }
}
.statistics {
  position: fixed;
  background-color: #fff;
  bottom: 0;
  left: 0;
  width: 100%;
  z-index: 100;
  border-top: 1rpx solid #ccc;
  display: flex;
  align-items: center;
  padding: 15rpx;
  .all-price {
    flex: 1;
    color: #666;
    text {
    }
  }

  .all-count {
  }
}
</style>